package br.com.mumakgnod.engine;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

public abstract class SpriteJFrame extends JFrame implements Runnable {
	private static final long serialVersionUID = -6964461500419506296L;
	public enum ANIMATION{ANDAR, PARAR, RECUAR, GOLPEAR, MAGIA};
	public enum HEADING{N, NE, E, SE, S, SO, O, NO};
	
	private ANIMATION actualAnim;
	private HEADING actualHeading;
	private BufferedImage image;
	private int rows, columns, frameSizeX, frameSizeY, actualFrame;
	
	
	public SpriteJFrame(File arquivo, int columns, int rows) {
		try {
			this.image = ImageIO.read(arquivo);
			this.rows = rows;
			this.columns = columns;
			
			this.frameSizeX = this.image.getWidth()/this.columns;
			this.frameSizeY = this.image.getHeight()/this.rows;
			
			setAnimation(ANIMATION.ANDAR);
			setHeading(HEADING.S);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	
	public void update() {
		switch (actualAnim) {
		case ANDAR:
			doAndar();
			break;
		case PARAR:
			doParar();
			break;
		case RECUAR:
			doRecuar();
			break;
		case GOLPEAR:
			doGolpear();
			break;
		case MAGIA:
			doMagia();
			break;
		}
	}
	
	public void onDraw(Object canvas, int posX, int posY) {
		Graphics2D canvas2D = (Graphics2D) canvas;
		
		update();

		int framePosX = (((actualFrame%columns==0)?(columns):(actualFrame%columns))-1) * frameSizeX;
		int framePosY = ((actualFrame%columns==0)?(actualFrame/columns)-1:(actualFrame/columns)) * frameSizeY;
		
		canvas2D.drawImage(image.getSubimage(framePosX, framePosY, frameSizeX, frameSizeY), posX, posY, this);
	}
	
	protected int getActualFrame() {
		return actualFrame;
	}
	
	public void setActualFrame(int actualFrame) {
		this.actualFrame = actualFrame;
	}
	
	protected ANIMATION getAnimation() {
		return actualAnim;
	}
	
	public void setAnimation(ANIMATION animation) {
		this.actualAnim = animation;
	}
	
	public HEADING getHeading() {
		return actualHeading;
	}
	
	public void setHeading(HEADING heading) {
		this.actualHeading = heading;
	}
	
	protected abstract void doAndar();
	protected abstract void doParar();
	protected abstract void doRecuar();
	protected abstract void doGolpear();
	protected abstract void doMagia();

}
